package com.learn.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.learn.springcloud.entity.Dept;
import com.learn.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
@RequestMapping("/dept")
public class DeptController {

	@Autowired
	private DeptService deptService;
	
	@GetMapping("/get/{id}")
	//一旦该服务方法失败并抛出了错误信息，会自动调用@HystrixCommand标注好的fallbackMethod指定的方法
	@HystrixCommand(fallbackMethod="processHystrix_Get")
	public Dept get(@PathVariable("id") Long id){
		Dept dept =  deptService.get(id);
		if(dept == null){
			throw new RuntimeException("该ID:"+id+"没有对应的信息；");
		}
		return dept;
	}
	
	public Dept processHystrix_Get(@PathVariable("id") Long id){
		Dept dept = new Dept();
		dept.setDeptno(id);
		dept.setDname("该ID:"+id+"没有对应的信息,null--@HystrixCommand");
		dept.setDb_source("no this database in mysql");
		return dept;
	}
}
